<?php

namespace App\Services;

use App\Models\WechatMenu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Class WechatMenuService
 * @package App\Services
 */
class WechatMenuService
{
    /**
     * @param Request $request
     * @return mixed
     */
    public static function add(Request $request)
    {
        $request->is_enable = 1;
        //添加菜单时先判断是添加主菜单还是子菜单，是主菜单的话，就要先判断当前活动下启用的主菜单个数
        if ($request->sub_id == 0) {
            $count = count(WechatMenu::activeCount($request->group_id));
            if ($count >= 3) {
                //此时只能添加 未启用 的主菜单
                $request->is_enable = 0;
            }
        }
        $wechatMenu = WechatMenu::create(array_merge($request->all(), [
            'is_enable' => $request->is_enable
        ]));
        return $wechatMenu;
    }

    /**
     * @param WechatMenu $wechatMenu
     * @return WechatMenu|string
     * @throws \Exception
     */
    public static function edit(WechatMenu $wechatMenu)
    {
        $request = \request();
        $wechatMenu->fill($request->all());
        DB::beginTransaction();
        try {
            // 若为主菜单
            if ($request->sub_id == 0) {
                if ($request->is_enable == 0) {
                    //停用该顶层菜单下的所有子菜单
                    $isEnable = 0;
                } else {
                    //若已有三个开启的主菜单，则再开启主菜单时提示 最多只能开启三个主菜单
                    //先检测现有的开启的主菜单的个数
                    //并且要编辑的菜单id不在已启用的菜单id列表
                    $ids = WechatMenu::activeCount($request->group_id);
                    if (count($ids) >= 3 && !in_array($request->id, $ids)) {
                        //提示 最多只能开启三个主菜单
                        throw new \Exception('最多只能开启三个主菜单');
                    }
                    //启用该顶层菜单下的所有子菜单
                    $isEnable = 1;
                }
                // 含有子菜单
                if ($wechatMenu->children->count() > 0) {
                    // 更改子菜单状态
                    WechatMenu::where('sub_id', '=', $request->id)->update(['is_enable' => $isEnable]);
                }
            }
            $wechatMenu->save();
            DB::commit();
            return $wechatMenu;
        }catch (\Exception $exception) {
            DB::rollBack();
            throw $exception;
        }
    }

    /**
     * @param WechatMenu $wechatMenu
     * @return WechatMenu|string
     * @throws \Exception
     */
    public static function del(WechatMenu $wechatMenu)
    {
        DB::beginTransaction();
        try {
            if ($wechatMenu->children->count() > 0) {
                // 删除子菜单
                WechatMenu::where('sub_id', '=', $wechatMenu->id)->delete();
            }
            $wechatMenu->delete();
            DB::commit();
            return $wechatMenu;
        }catch (\Exception $exception) {
            DB::rollBack();
            return $exception->getMessage();
        }
    }
}